package com.liu.reflection;

import java.lang.reflect.Field;

public class Test {
    public static void main(String[] args) {
        Class<?> c1 = getClass("com.liu.reflection.pojo.User");

        // 一个类在内存中只有一个class对象
        Class<?> c2 = getClass("com.liu.reflection.pojo.User");
        Class<?> c3 = getClass("com.liu.reflection.pojo.User");
        System.out.println(c1.hashCode());
        System.out.println(c2.hashCode());
        System.out.println(c3.hashCode());

        //一个类被加载后，类的整个结构都会被封装在class对象中
        c1.getClass();

    }



    /**
     * 通过反射获取Class对象
     * @param clazzPath
     * @return
     */
    private static Class<?> getClass(String clazzPath) {
        Class<?> aClass = null;
        try {
            aClass = Class.forName(clazzPath);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return aClass;
    }

    /**
     * 通过反射获取属性
     * @param clazz
     * @return
     */
    private static String[] fieldName(Class clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();
        String[] fieldNames = new String[declaredFields.length];
        for (int i = 0; i < declaredFields.length; i++) {
            fieldNames[i] = declaredFields[i].getName();
        }
        return fieldNames;
    }
}
